﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using CARMSBusiness;
using CARMSData.CollectionClasses;
using CARMSData.EntityClasses;
using CARMSData.HelperClasses;
using SD.LLBLGen.Pro.ORMSupportClasses;
using VitalShining.WebControls;

namespace CARMS.Report
{
    [Function("Report.MyReportHistory")]
    public partial class MyReportHistory : PageBase
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
               
                    btnNewItem.Enabled = true;
          
                GenerateGridColumnByActionID(gridList);
                PageQueryAdapter1.PerformQuery();
            }
        }

 

        protected override void btnNewItem_Click(object sender, EventArgs e)
        {
             RediretResolveUrl(SiteUrls.Report.ReportSelect);
        }

        protected void PageQueryAdapter1_PerformGetCondition(object sender, PerformGetConditionEventArgs e)
        {
            PredicateExpression Condition = new PredicateExpression();

            //if (txtLoginName.Text.Trim().Length > 0)
            //{
            //    Condition.Add(UserFields.LoginName % ("%" + txtLoginName.Text.Trim() + "%"));
            //}
            //if (txtFullName.Text.Trim().Length > 0)
            //{
            //    Condition.Add(UserFields.FullName % ("%" + txtFullName.Text.Trim() + "%"));
            //}
            //if (chlRoles.SelectedValue.Trim().Length > 0)
            //{
            //    var userRoleCollection = new UserRoleCollection();
            //    userRoleCollection.GetMulti(UserRoleFields.RoleId == new Guid(chlRoles.SelectedValue));

            //    var list = from item in userRoleCollection select item.UserId;

            //    var compareExpression = new FieldCompareRangePredicate(UserFields.UserId, false, list.ToArray());
            //    Condition.Add(compareExpression);
            //}
            //Condition.Add(UserFields.IsActived == true);
            Condition.Add(ReportHeaderFields.ApplyUserId == CurrentLoginUser.UserID);
            Condition.Add(ReportHeaderFields.IsActive == true);
            e.Condition = Condition;
        }

        protected void PageQueryAdapter1_PerformGetRecordCount(object sender, PerformGetRecordCountEventArgs e)
        {
            var collection = new ReportHeaderCollection(); 
            e.RecordCount = collection.GetDbCount((PredicateExpression)e.Condition);
        }

        protected void PageQueryAdapter1_PerformGetPageData(object sender, PerformGetPageDataEventArgs e)
        {
            SortExpression sort;
            switch (gridList.SortExpression)
            {
                //case "LoginName":
                //    sort = new SortExpression(UserFields.LoginName | (SortOperator)(int)gridList.SortDirection);
                //    break;
                //case "FullName":
                //    sort = new SortExpression(UserFields.FullName | (SortOperator)(int)gridList.SortDirection);
                //    break;
                //case "Phone":
                //    sort = new SortExpression(UserFields.Tel | (SortOperator)(int)gridList.SortDirection);
                //    break;

                //case "CreateDate":
                //    sort = new SortExpression(UserFields.CreateDate | (SortOperator)(int)gridList.SortDirection);
                //    break;
                //case "Status":
                //    sort = new SortExpression(UserFields.IsActived | (SortOperator)(int)gridList.SortDirection);
                //    break;
                default:
                    sort = new SortExpression(ReportHeaderFields.CreateDate | SortOperator.Descending);
                    break;
            }

            var collection = new ReportHeaderCollection();
            collection.GetMulti((PredicateExpression)e.Condition,0, sort,null,null, e.PageIndex + 1, e.PageSize);
            e.DataSource = collection;
  
        }

        protected void gridList_RowDataBound(object sender, GridViewRowEventArgs e)
        {
            if (e.Row.RowType == DataControlRowType.DataRow)
            {
                var data = (ReportHeaderEntity)e.Row.DataItem; 
                //var rptRoles = (Repeater)e.Row.FindControl("rptRoles");
                //if (rptRoles != null)
                //{
                //    var roles = SecurityBusiness.GetRoleListByUserID(data.UserId);

                //    rptRoles.DataSource = roles;
                //    rptRoles.DataBind();
                //}
                //e.Row.Attributes["UserId"] = data.UserId.ToString();
            }
        }

        protected void gridList_RowCommand(object sender, GridViewCommandEventArgs e)
        {
            switch (e.CommandName)
            {
                case "ViewItem":
                    var reportHeader = new ReportHeaderEntity(int.Parse(e.CommandArgument.ToString()));
                    RediretResolveUrl(reportHeader.ViewUrl);
                    break;
            }
        }
    }
}
